{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "train=pd.read_csv('train_MLWARE2.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ID</th>\n",
       "      <th>userId</th>\n",
       "      <th>itemId</th>\n",
       "      <th>rating</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>16041_129</td>\n",
       "      <td>16041</td>\n",
       "      <td>129</td>\n",
       "      <td>0.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>16041_25</td>\n",
       "      <td>16041</td>\n",
       "      <td>25</td>\n",
       "      <td>0.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>16041_28</td>\n",
       "      <td>16041</td>\n",
       "      <td>28</td>\n",
       "      <td>5.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>16041_101</td>\n",
       "      <td>16041</td>\n",
       "      <td>101</td>\n",
       "      <td>0.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>16041_47</td>\n",
       "      <td>16041</td>\n",
       "      <td>47</td>\n",
       "      <td>1.5</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          ID  userId  itemId  rating\n",
       "0  16041_129   16041     129     0.5\n",
       "1   16041_25   16041      25     0.5\n",
       "2   16041_28   16041      28     5.5\n",
       "3  16041_101   16041     101     0.5\n",
       "4   16041_47   16041      47     1.5"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "test=pd.read_csv('test_MLWARE2.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "test_id=test['ID']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Give test a rating for libfm needs it. Assume it is 5.\n",
    "test['rating']=5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ID</th>\n",
       "      <th>userId</th>\n",
       "      <th>itemId</th>\n",
       "      <th>rating</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>16041_10</td>\n",
       "      <td>16041</td>\n",
       "      <td>10</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>16041_107</td>\n",
       "      <td>16041</td>\n",
       "      <td>107</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>16041_1</td>\n",
       "      <td>16041</td>\n",
       "      <td>1</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>16041_40</td>\n",
       "      <td>16041</td>\n",
       "      <td>40</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>16041_96</td>\n",
       "      <td>16041</td>\n",
       "      <td>96</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          ID  userId  itemId  rating\n",
       "0   16041_10   16041      10       5\n",
       "1  16041_107   16041     107       5\n",
       "2    16041_1   16041       1       5\n",
       "3   16041_40   16041      40       5\n",
       "4   16041_96   16041      96       5"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "train.drop('ID',axis=1,inplace=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "test.drop('ID',axis=1,inplace=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "train.to_csv('../scripts/train.txt',header=None,index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "test.to_csv('../scripts/test.txt',header=None,index=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "#After libFM has trained and predicted,make the result the same file."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "sub=pd.read_csv('./sample_submission_MLWARE2.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "sub.ID=test_id"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "prediction=pd.read_csv('./predict.libfm',header=None)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "sub.rating=prediction"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "sub.to_csv('Submission.csv',index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ID</th>\n",
       "      <th>rating</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>16041_10</td>\n",
       "      <td>2.950720</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>16041_107</td>\n",
       "      <td>0.889114</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>16041_1</td>\n",
       "      <td>3.949740</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>16041_40</td>\n",
       "      <td>6.212190</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>16041_96</td>\n",
       "      <td>5.055220</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>16041_137</td>\n",
       "      <td>7.757690</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>16041_51</td>\n",
       "      <td>4.355690</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>16041_59</td>\n",
       "      <td>6.214210</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>16041_135</td>\n",
       "      <td>5.987330</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>16041_15</td>\n",
       "      <td>6.398800</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>16041_48</td>\n",
       "      <td>7.024300</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>16041_83</td>\n",
       "      <td>7.611790</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>16041_24</td>\n",
       "      <td>7.219570</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>16041_77</td>\n",
       "      <td>5.976460</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>16041_70</td>\n",
       "      <td>5.823540</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>16041_121</td>\n",
       "      <td>7.608070</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>16041_76</td>\n",
       "      <td>7.402290</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>16041_109</td>\n",
       "      <td>2.984490</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>16041_35</td>\n",
       "      <td>7.489480</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>16041_128</td>\n",
       "      <td>7.512170</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20</th>\n",
       "      <td>16041_62</td>\n",
       "      <td>6.533550</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21</th>\n",
       "      <td>16041_127</td>\n",
       "      <td>7.146670</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22</th>\n",
       "      <td>16041_82</td>\n",
       "      <td>6.350780</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23</th>\n",
       "      <td>16041_108</td>\n",
       "      <td>7.133460</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>24</th>\n",
       "      <td>56092_25</td>\n",
       "      <td>6.584000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25</th>\n",
       "      <td>56092_10</td>\n",
       "      <td>7.002020</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>26</th>\n",
       "      <td>56092_107</td>\n",
       "      <td>4.997370</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>27</th>\n",
       "      <td>56092_47</td>\n",
       "      <td>5.324390</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>28</th>\n",
       "      <td>56092_21</td>\n",
       "      <td>6.186270</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29</th>\n",
       "      <td>56092_63</td>\n",
       "      <td>6.007190</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>640985</th>\n",
       "      <td>1587_2</td>\n",
       "      <td>8.235330</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>640986</th>\n",
       "      <td>1587_11</td>\n",
       "      <td>9.115940</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>640987</th>\n",
       "      <td>1587_90</td>\n",
       "      <td>9.043470</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>640988</th>\n",
       "      <td>1587_14</td>\n",
       "      <td>9.180990</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>640989</th>\n",
       "      <td>1587_22</td>\n",
       "      <td>8.373160</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>640990</th>\n",
       "      <td>1587_23</td>\n",
       "      <td>8.469460</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>640991</th>\n",
       "      <td>1587_79</td>\n",
       "      <td>7.792430</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>640992</th>\n",
       "      <td>1587_97</td>\n",
       "      <td>7.589000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>640993</th>\n",
       "      <td>1587_44</td>\n",
       "      <td>8.446600</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>640994</th>\n",
       "      <td>1587_110</td>\n",
       "      <td>7.071480</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>640995</th>\n",
       "      <td>1587_126</td>\n",
       "      <td>8.146310</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>640996</th>\n",
       "      <td>1587_86</td>\n",
       "      <td>7.388690</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>640997</th>\n",
       "      <td>1587_88</td>\n",
       "      <td>7.668520</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>640998</th>\n",
       "      <td>1587_74</td>\n",
       "      <td>6.678240</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>640999</th>\n",
       "      <td>1587_16</td>\n",
       "      <td>7.801010</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>641000</th>\n",
       "      <td>1587_12</td>\n",
       "      <td>7.241550</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>641001</th>\n",
       "      <td>1587_15</td>\n",
       "      <td>8.326780</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>641002</th>\n",
       "      <td>1587_41</td>\n",
       "      <td>8.118540</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>641003</th>\n",
       "      <td>1587_75</td>\n",
       "      <td>7.137200</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>641004</th>\n",
       "      <td>1587_70</td>\n",
       "      <td>7.883640</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>641005</th>\n",
       "      <td>1587_27</td>\n",
       "      <td>7.686300</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>641006</th>\n",
       "      <td>1587_38</td>\n",
       "      <td>6.635930</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>641007</th>\n",
       "      <td>1587_33</td>\n",
       "      <td>7.407360</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>641008</th>\n",
       "      <td>1587_46</td>\n",
       "      <td>6.273610</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>641009</th>\n",
       "      <td>1587_58</td>\n",
       "      <td>7.125660</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>641010</th>\n",
       "      <td>1587_6</td>\n",
       "      <td>6.929870</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>641011</th>\n",
       "      <td>1587_122</td>\n",
       "      <td>6.366860</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>641012</th>\n",
       "      <td>1587_34</td>\n",
       "      <td>7.589680</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>641013</th>\n",
       "      <td>1587_80</td>\n",
       "      <td>4.825530</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>641014</th>\n",
       "      <td>1587_5</td>\n",
       "      <td>5.150770</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>641015 rows × 2 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "               ID    rating\n",
       "0        16041_10  2.950720\n",
       "1       16041_107  0.889114\n",
       "2         16041_1  3.949740\n",
       "3        16041_40  6.212190\n",
       "4        16041_96  5.055220\n",
       "5       16041_137  7.757690\n",
       "6        16041_51  4.355690\n",
       "7        16041_59  6.214210\n",
       "8       16041_135  5.987330\n",
       "9        16041_15  6.398800\n",
       "10       16041_48  7.024300\n",
       "11       16041_83  7.611790\n",
       "12       16041_24  7.219570\n",
       "13       16041_77  5.976460\n",
       "14       16041_70  5.823540\n",
       "15      16041_121  7.608070\n",
       "16       16041_76  7.402290\n",
       "17      16041_109  2.984490\n",
       "18       16041_35  7.489480\n",
       "19      16041_128  7.512170\n",
       "20       16041_62  6.533550\n",
       "21      16041_127  7.146670\n",
       "22       16041_82  6.350780\n",
       "23      16041_108  7.133460\n",
       "24       56092_25  6.584000\n",
       "25       56092_10  7.002020\n",
       "26      56092_107  4.997370\n",
       "27       56092_47  5.324390\n",
       "28       56092_21  6.186270\n",
       "29       56092_63  6.007190\n",
       "...           ...       ...\n",
       "640985     1587_2  8.235330\n",
       "640986    1587_11  9.115940\n",
       "640987    1587_90  9.043470\n",
       "640988    1587_14  9.180990\n",
       "640989    1587_22  8.373160\n",
       "640990    1587_23  8.469460\n",
       "640991    1587_79  7.792430\n",
       "640992    1587_97  7.589000\n",
       "640993    1587_44  8.446600\n",
       "640994   1587_110  7.071480\n",
       "640995   1587_126  8.146310\n",
       "640996    1587_86  7.388690\n",
       "640997    1587_88  7.668520\n",
       "640998    1587_74  6.678240\n",
       "640999    1587_16  7.801010\n",
       "641000    1587_12  7.241550\n",
       "641001    1587_15  8.326780\n",
       "641002    1587_41  8.118540\n",
       "641003    1587_75  7.137200\n",
       "641004    1587_70  7.883640\n",
       "641005    1587_27  7.686300\n",
       "641006    1587_38  6.635930\n",
       "641007    1587_33  7.407360\n",
       "641008    1587_46  6.273610\n",
       "641009    1587_58  7.125660\n",
       "641010     1587_6  6.929870\n",
       "641011   1587_122  6.366860\n",
       "641012    1587_34  7.589680\n",
       "641013    1587_80  4.825530\n",
       "641014     1587_5  5.150770\n",
       "\n",
       "[641015 rows x 2 columns]"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sub"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [Root]",
   "language": "python",
   "name": "Python [Root]"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
